<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input
        placeholder="用户名手机"
        v-model="form.username"
        @focus="hideErrMsg('username')"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input
        placeholder="验证码"
        v-model="form.captcha"
        @focus="hideErrMsg('captcha')"
      >
        <template slot="append">
          <el-button @click="handleSendCaptcha"> 发送验证码 </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input
        placeholder="你的名字"
        v-model="form.nickname"
        @focus="hideErrMsg('nickname')"
      >
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="hideErrMsg('password')"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input
        placeholder="确认密码"
        type="password"
        v-model="form.checkPassword"
        @focus="hideErrMsg('checkPassword')"
      >
      </el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">
      注册
    </el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请再次密码"));
      } else if (value != this.form.password) {
        callback(new Error("两次输入密码不匹配"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "17777552458", // 登录用户名/手机
        password: "123456", // 登录密码
        captcha: "000000", // 手机验证码
        nickname: "范二", //昵称
        checkPassword: "123456", //确认密码
      },
      // 表单规则
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名/手机",
            trigger: "blur",
          },
          {
            pattern: /^[1][3-9][0-9]{9}$/,
            message: "手机号码应为11位",
            trigger: "blur",
          },
        ],
        captcha: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            message: "密码必须是6位以上",
            trigger: "blur",
          },
        ],
        nickname: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur",
          },
        ],
        checkPassword: [
          {
            //   验证规则里面, 有三个需要处理的变量
            //   1. 要求
            //   2. 提示语
            //   3. 触发
            // 最灵活的是一个函数校验形式, 只剩下触发条件需要声明
            // 其余全部放到函数 validator 里面
            //   required: true,
            //   message: '该项不能为空',
            validator: validatePass,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    $router(n, o) {
      if (n.fullPath !== o.fullPath) {
        //监听路由参数是否变化
        this.getData(); //methods中封装的加载数据函数
      }
    },
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      this.$axios({
        method: "post",
        url: "/captchas",
        data: {
          tel: this.form.username,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.code) {
          this.$message.success("验证码是" + res.data.code);
        }
      });
    },

    // 注册
    handleRegSubmit(index) {
      console.log(this.form);
      this.$refs.form
        .validate()
        .then((res) => {
          // 先用剩余参数语法将 checkPassword 排除掉
          const { checkPassword, ...data } = this.form;
          this.$axios({
            method: "POST",
            url: "/accounts/register",
            data,
          })
            .then((res) => {
              console.log(res.data);
              //注册成功
              this.$message.success("注册成功");
              // 自动登录  把数据存放到vuex里
              // this.$store.commit("user/setUserInfo", res.data);

              // 路由跳转页面
              // 或者  location.reload() 直接刷新页面
              this.$router.go(0);
            })
            .catch((err) => {
              console.dir(err);
            });
        })
        .catch((res) => {
          console.log("校验失败");
        });
    },
    //有光标关闭提示
    hideErrMsg(propName) {
      // 拿到当前表单
      // 调用清理错误信息的函数
      // 传入需要清理的 prop
      this.$refs.form.clearValidate(propName);
    },
  },
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>